//app.js
App({
  globalData: {
    qqmapsdk:"",
    code: "",
    userInfo: '',
    openid:"",
   // url: "http://localhost:8080/",
    url: "http://r8rejok247.52http.net/",
    longitude: '',
    latitude:'',
    id: '',
    detailed:'',
    address: '',
    marketinfo:[],
    vtype:0,
    toDialogue:''
  },
  onLaunch: function () {
    
    var that = this
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)


    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        that.globalData.code = res.code
        wx.request({
          url: that.globalData.url + "weChat/weChatOpen" ,
          data: {
            code: res.code
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            that.globalData.openid = res.data.openid
          }
        })
       
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
          var  result=  this.requsetLogin("User/login", that.globalData.openid, that.globalData.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况     后续改动     重要
              if (result) {
                this.userInfoReadyCallback(result)
              }
            }
          })
        }
      }
    })
  },
  requsetLogin: function (url, openid,user) {
    console.log(openid)
     var that = this;
      wx.request({
        url: that.globalData.url + url,
        data: {
          openid: openid,
          userName: user.nickName,
          sex: user.gender,
          status:"1",
          remarks: user.avatarUrl,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          if (res.data==null){
            that.onLaunch(); 
          }else{
            that.globalData.userInfo=res.data
          }
          return res
        }
      })
    },
    isUserInfo:function(){
      return app.globalData.userInfo==null
    },
    require:function(){
   var   QQMapWX = require('/pages/libs/qqmap-wx-jssdk1.2.js');
     this.globalData.qqmapsdk = new QQMapWX({
          key: 'IM6BZ-HZORR-TRQW6-WUC5W-SDAWE-W4F5Q'
        })
      return this.globalData.qqmapsdk
    }
})